<template>
  <!-- 政策法规 -->
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="发布类型" prop="informationType">
        <el-select
          v-model="queryParams.informationType"
          placeholder="请选择发布类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in typeData"
            :key="dict.id"
            :label="dict.informationTypeName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布目标" prop="publishTarget">
        <el-select
          v-model="queryParams.publishTarget"
          placeholder="请选择发布目标"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in inforTypeList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否已读" prop="isRead">
        <el-select
          v-model="queryParams.isRead"
          placeholder="请选择是否已读"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in isReadList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="查询标题" prop="publishTitle">
        <el-input
          v-model="queryParams.publishTitle"
          placeholder="请输入查询标题"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="政策法规标题" align="center" prop="publishTitle" :show-overflow-tooltip="true">
        <!--        <template slot-scope="scope">-->
        <!--          <span class="title-sty" @click="viewDetail(scope.row)">{{ scope.row.publishTitle }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="类型" align="center" prop="informationType" :show-overflow-tooltip="true" />
      <el-table-column label="发布目标" align="center" prop="releaseScope" :show-overflow-tooltip="true" />
      <el-table-column label="发布时间" align="center" prop="releaseTime" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="是否已读" align="center" prop="isRead" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.isRead == false ? '未读' : '已读' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已读时间" align="center" prop="readTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewDetail(scope.row)"
          >查询</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 查看详情对话框 -->
    <el-dialog title="查询详情" :visible.sync="detail" width="800px" append-to-body :close-on-click-modal="false" :before-close="closeAdd">
      <div class="dialog-content">
        <div class="title-content">
          <div>
            <span class="content-span">{{ detailData.publishTitle }}</span>
            <span v-if="detailData.informationType !== undefined">{{ '(' + detailData.informationType + ')' }}</span>
          </div>
        </div>
        <div class="release-conteng">
          <div style="padding-right: 20px">发布人：{{ detailData.pubByName }}</div>
          <div style="padding-left: 20px">发布时间：{{ detailData.releaseTime }}</div>
        </div>
        <hr>
        <div class="content-detail ql-editor" v-html="detailData.pubContent" />
        <div class="file-content">
          <div class="file-title">附件：</div>
          <div class="file-name-content">
            <div v-for="item in fileData" class="file-name" @click="downloadFile(item)">{{ item.fileName + ' ' }}</div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { informType, reguDetail, regulList } from '@/api/policiesRegul'

export default {
  name: 'Index',
  data() {
    return {
      fileData: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 发布类型字典
      typeData: [],
      // 详情模态框状态
      detail: false,
      // 筛选条件字段
      queryParams: {
        informationType: '',
        publishTarget: '',
        isRead: '',
        publishTitle: '',
        pageNum: 1,
        pageSize: 10
      },
      // 类型标签字典
      inforTypeList: [
        {
          label: '所有企业',
          value: 'ALL'
        },
        {
          label: '本企业',
          value: 'SELF'
        }
      ],
      // 是否已读字典
      isReadList: [
        {
          label: '已读',
          value: true
        },
        {
          label: '未读',
          value: false
        }
      ],
      // 总条数
      total: 0,
      // 表格数据
      typeList: [],
      detailData: {
        pubByName: '', // 发布人
        publishTitle: '', // 发布标题
        pubContent: '', // 发布内容
        releaseTime: '', // 发布时间
        informationType: '', // 类型
        files: [] // 文件
      }
    }
  },
  created() {
    this.getInformType()
    this.getList()
  },
  methods: {
    /** 获取发布类型字典 */
    getInformType() {
      informType().then(response => {
        this.typeData = response.data
      })
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true
      regulList(this.addDateRange(this.queryParams)).then(response => {
        this.typeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 下载文件 */
    downloadFile(data) {
      console.log('data', data)
      window.open(process.env.VUE_APP_RESOURCE_PREFIX + data.path)
    },
    /** 查看详情 */
    viewDetail(row) {
      console.log('row', row)
      reguDetail(row.id).then(response => {
        this.detailData = response.data
        this.detailData.pubContent = response.data.pubContent.replace(/<p\s*\/?>/g, '')
        this.detailData.pubContent = this.detailData.pubContent.replace(/<br\s*\/?>/g, '')
        this.detailData.pubContent = this.detailData.pubContent.replace(/<\s*\/?p>/g, '')
        this.fileData = response.data.files
        console.log('response.data.files', response.data.files)
        this.detail = true
      })
    },
    closeAdd() {
      this.getList()
      this.detail = false
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      console.log('selection', selection)
      this.ids = selection.map(item => item.id)
      console.log('ids', this.ids)

      // this.single = selection.length !== 1
      // this.multiple = !selection.length
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('policies/export', {
        ...this.queryParams, ids: this.ids
      }, `政策法规.xlsx`)
    }

  }

}
</script>

<style lang="scss" scoped>
.title-sty {
  color: #20a0ff;
  cursor: pointer
}
.dialog-content {
  .title-content {
    text-align: center;
    margin-bottom: 14px;
    .content-span {
      font-size: 18px;
      font-weight: bold;
      display: inline
    }
  }
  .content-detail {
    margin-bottom: 14px;
    height: 300px;
    overflow-y: scroll;
  }
  .file-content {
    display: inline-block;
    .file-title {
      display: inline-block;
      vertical-align: top;
    }
    .file-name-content {
      display: inline-block;
      .file-name {
        color: #20a0ff;
        cursor: pointer;
        margin-bottom: 4px
      }
    }
  }
  .release-conteng {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    margin-bottom: 14px
  }
}
</style>
